/**
 * Created by Forcs on 16/4/6.
 */
var RouteService = function (app) {
  var self = this
  self._routes = []
  self._app = app
}

RouteService.prototype.current = function () {
  var self = this
  if (self._routes) {
    return self._routes[self._routes.length - 1].item
  } else {
    return undefined
  }
}

RouteService.prototype.back = function () {
  var self = this
  if (self._routes) {
    self._routes.pop()
  }
}

RouteService.prototype.goto = function () {
  var self = this
  let item = {}
  item.name = arguments[0]
  if (arguments.length > 1) {
    item.options = arguments[1]
  }

  if (!self._routes.length) {
    self._routes.push({
      name: item.name,
      item: item
    })
  } else {
    let len = self._routes.length
    let pos = -1
    for (let i = len - 1; i >= 0; i--) {
      let n = self._routes[i].name
      if (n === item.name) {
        pos = i
        break
      }
    }

    let count = self._routes.length - pos
    if (count > 0 && count <= self._routes.length) {
      self._routes.splice(pos, count)
    }
    self._routes.push({
      name: item.name,
      item: item
    })
  }
}

export default RouteService
